package com.stottlerhenke.simbionic.samples.beyondhello;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.ArrayList;

import com.stottlerhenke.simbionic.api.SB_Config;
import com.stottlerhenke.simbionic.api.SB_Engine;
import com.stottlerhenke.simbionic.api.SB_Error;

public class BeyondHello
{

	public static void main(String[] args)
	{
		// default configuration
		int num_entities = 1;
		int num_updates = 20;
		String starting_behavior = "Nested";
		String host = "localhost";
		
		// parse command-line arguments
		//	-e #				number of entities to create
		//	-n #				number of updates to run on entities
		//	-b <behavior name>	behavior to run for all entities
		//	-h <host name>		network host
		int argc = args.length;
		for (int i = 0; i < argc; ++i)
		{
			String arg = args[i];
			if (arg.length() >= 2 && arg.charAt(0) == '-' && i + 1 < argc)
			{
				switch (arg.charAt(1))
				{
				case 'e':
					num_entities = Integer.parseInt(args[i + 1]);
					++i;
					break;
				case 'n':
					num_updates = Integer.parseInt(args[i + 1]);
					++i;
					break;
				case 'b':
					starting_behavior = args[i + 1];
					++i;
					break;
				case 'h':
					host = args[i + 1];
					++i;
					break;
				}
			}
		}
		BeyondHello_Interface beyondHello_Interface = new BeyondHello_Interface();

	    // create the object that holds configuration parameters for the engine
		SB_Config myConfig = new SB_Config();

		// specify the name of the SIM file to load
		try
		{
			
			File file = new File("samples/BeyondHello/BeyondHello.sim");
			myConfig.fileURL = new URL("file", "localhost", file.getAbsolutePath());
			myConfig.debugEnabled = false;
		}
		catch(Exception e)
		{
			System.out.println("Exception creating URL from the sim file name.");
		}

		// creates an instance of the engine interface object
		SB_Engine engine = new SB_Engine();

		// specify the log file
		PrintStream logPrintStream = null;
		try
		{
			logPrintStream = new PrintStream( new FileOutputStream("BeyondHello.log") );
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

		if( logPrintStream != null )
		{
			engine.registerLogPrintStream( logPrintStream );
		}

		// initialize the engine
		if (engine.initialize(beyondHello_Interface, myConfig) != SB_Error.kOK)
		{
			System.out.println("Engine failed to initialize: " + engine.getLastError());
			return;
		}

		for (int i = 0; i < num_entities; ++i)
		{
			// create an entity
			long entityId = engine.makeEntity( "Entity " + (i + 1) );
			if (entityId == -1 )
			{
				System.out.println("Entity creation error: " + engine.getLastError());
				return;
			}

			// set entity's initial behavior
			SB_Error errCode = engine.setBehavior( entityId, starting_behavior, new ArrayList());
			if (errCode != SB_Error.kOK)
			{
				System.out.println("Error setting behavior: " + engine.getLastError());
				return;
			}
			
			beyondHello_Interface.initializeSimState(entityId);
		}
		
		// update the entity several times
		for (int tick=0; tick < num_updates; tick++)
		{
			if (engine.update() != SB_Error.kOK)
			{
				System.out.println("Update error: " + engine.getLastError());
				return;
			}
		}

		engine.terminate();     // shut down the engine
	}
}
